package cn.fyg.service.finance.domain.accountdetail.impl;

import java.io.StringReader;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader;

import cn.fyg.common.base.retv.Retv;
import cn.fyg.service.finance.config.Connect;
import cn.fyg.service.finance.domain.accountdetail.AccountdetailService;
import cn.fyg.service.finance.domain.accountdetail.BProjectDto;
import cn.fyg.service.finance.domain.accountdetail.BalanceDto;
import cn.fyg.service.finance.domain.accountdetail.ProjectdetailDto;
import cn.fyg.service.finance.domain.accountdetail.ProjectsDto;
import cn.fyg.service.finance.domain.salary.SearchBean;
import cn.fyg.service.finance.util.DES;
import cn.fyg.service.finance.util.GsonUtil;

@Component
public class AccountdetailServiceImpl implements AccountdetailService {
	public static final String REST_SERVICE_URI = new Connect().getUrl();

	private String doPost(SearchBean bean) {
		RestTemplate restTemplate = new RestTemplate();

		String result = "";
		if (bean.getProjectid() != null) {
			String result_encrypted = restTemplate.postForObject(REST_SERVICE_URI
					+ "/getProjcetDetail?accountid={accountid}&projectid={projectid}&begYear=2008&begMonth=12&begDate=20&endYear=2150&endMonth=12&endDate=31&amountType=project&fyg=fyg",
					null, String.class, bean.getAccountid(), bean.getProjectid());
			try {
				result = DES.decryptDES(result_encrypted, DES.key);
			} catch (Exception e) {
				System.out.println("error");
			}
		} else {
			String result_encrypted = restTemplate.postForObject(REST_SERVICE_URI
					+ "/getAccountAndProjcets?accountid={accountid}&accountnumber={accountnumber}&begYear=2008&begMonth=12&begDate=20&endYear=2150&endMonth=12&endDate=31&fyg=fyg",
					null, String.class, bean.getAccountid(), bean.getAccountnumber());
			try {
				result = DES.decryptDES(result_encrypted, DES.key);
			} catch (Exception e) {
				System.out.println("error");
			}
		}
		return result;
	}

	@Override
	public Retv<BalanceDto> getAccountAndProjcets(String accountid, String accountnumber) {
		// TODO Auto-generated method stub
		SearchBean bean = new SearchBean();
		bean.setAccountid(accountid);
		bean.setAccountnumber(accountnumber);
		bean.setProjectnumber("");
		String result = this.doPost(bean);
		if (result.contains("projects")) {
			JsonElement je = Streams.parse(new JsonReader(new StringReader(result)));
			JsonObject jo = je.getAsJsonObject();
			List<ProjectsDto> detail = GsonUtil.fromJsonList(jo.get("projects").toString(), ProjectsDto.class);
			BalanceDto balance = new BalanceDto();
			balance.setProjects(detail);
			balance.setBalance(jo.get("balacne").toString());
			return Retv.data(balance);
		}
		return Retv.error("当前账户下没有数据");
	}

	@Override
	public Retv<BProjectDto> getProjcetDetail(String accountid, String projectid) {
		// TODO Auto-generated method stub
		SearchBean bean = new SearchBean();
		bean.setAccountid(accountid);
		bean.setProjectid(projectid);
		String result = this.doPost(bean);
		if (result.contains("projects")) {
			JsonElement je = Streams.parse(new JsonReader(new StringReader(result)));
			JsonObject jo = je.getAsJsonObject();
			List<ProjectdetailDto> detail = GsonUtil.fromJsonList(jo.get("projects").toString(), ProjectdetailDto.class);
			BProjectDto project = new BProjectDto();
			project.setProjects(detail);
			project.setPeriodBalance(jo.get("periodBalance").toString());
			project.setPeriodCreditamount(jo.get("periodCreditamount").toString());
			project.setPeriodDebitamount(jo.get("periodDebitamount").toString());
			return Retv.data(project);
		}
		return Retv.error("当前项目下没有数据");
	}

}
